﻿using KINLO.Model;
using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace KINLO.Data.Reporitory
{
    public class LogReporitory
    {
        private static readonly log4net.ILog log = log4net.LogManager.GetLogger(typeof(WeightReporitory));
        Database DB;
        public LogReporitory()
        {
            DB = (new DbContext("name=WeightDB")).Database;
        }

        public List<HistoryEvent> GetLogEventInfo(DateTime start,DateTime end,string lvl)
        {
            var cmd = @"SELECT Id ,Date ,Thread ,Level ,Logger ,Message ,Exception FROM dbo.LOG
                        WHERE Date BETWEEN @StartDate AND @EndDate AND (Level=@Level OR 'ALL'=@Level)";
            return DB.SqlQuery<HistoryEvent>(cmd,
                 new SqlParameter("@StartDate", start),
                 new SqlParameter("@EndDate", end),
                 new SqlParameter("@Level", lvl)
                ).ToList();
        }

        public int DeleteLogInfo(string id)
        {
            var cmd = @"DELETE dbo.Log
                         WHERE Id=@ID";
            return DB.ExecuteSqlCommand(cmd,
                new SqlParameter("@ID", id));
        }
        public int DeleteLogInfos(string id)
        { 
               var cmd = @"EXEC dbo.ProcDelLog @logids";
            var a= DB.ExecuteSqlCommand(cmd,
                new SqlParameter("@logids", id));
            return a;
        }
        public int DeleteLogByDays(int day)
        {
            var cmd = @"EXEC dbo.ClearLogByDay @day";
            var a = DB.ExecuteSqlCommand(cmd,
                new SqlParameter("@day", day));
            return a;
        }
        
    }
}
